/**
 * 
 */
package com.mobile2you.nci.android;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.util.Linkify;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.mobile2you.nci.android.data.InitialDataLoader;
import com.mobile2you.nci.android.data.SqlHelper;
import com.mobile2you.nci.android.data.WebServicesRequest;
import com.mobile2you.nci.android.domain.Centres;
import com.mobile2you.nci.android.utils.BulleDialog;
import com.mobile2you.nci.android.utils.ListCentreAdapter;
import com.mobile2you.nci.android.utils.LoadImagePool;
import com.mobile2you.nci.android.utils.Utils;
import com.mobile2you.nci.android.view.LoaderView;

/**
 * @author JBromo
 * 
 */
public class MenuCentre extends MapActivity implements
		BulleDialog.onBullClickListener {

	List<Centres> listCentres = new ArrayList<Centres>();

	private ListView listView;
	private MapView mapView;

	View mapMode;
	View directionMode;
	View listMode;

	ViewFlipper viewFlipper;

	ListCentreAdapter adapter;

	private TextView titleCentres;

	boolean fromMapMode;

	private static final int TITLE_MAIN = 0;

	private static final int TITLE_MAP = 1;

	private static final int TITLE_LIST = 2;

	private static final int TITLE_DIRECTION = 3;

	ImageView imageView;

	TextView textCentre;

	View waitingImage;

	LoaderView loaderView;

	private class UpdateLoadCentresTask extends
			AsyncTask<String, String, List<Centres>> {

		@Override
		protected List<Centres> doInBackground(String... params) {
			boolean toupdate = SqlHelper.getInstance(MenuCentre.this).toUpdate(
					SqlHelper.KEY_CENTRE);
			if (toupdate) {
				listCentres = WebServicesRequest.getCentres(MenuCentre.this);
			}
			if (listCentres == null || listCentres.isEmpty()) {
				listCentres = SqlHelper.getInstance(getApplicationContext())
						.getLocalCentres();
				if (listCentres.isEmpty()) {
					listCentres = InitialDataLoader.getCentres(MenuCentre.this);
				}
			}
			return listCentres;
		}

		@Override
		protected void onPostExecute(List<Centres> result) {
			super.onPostExecute(result);
			if (result != null && !result.isEmpty()) {
				listCentres = result;
				showListview();
				showMapview();
				initViews();
				viewFlipper.removeView(loaderView);
			} else {
				loaderView.setErrorMessage();
			}

		}
	}

	private void initViews() {

		imageView = (ImageView) findViewById(R.id.img_centre_detail);
		waitingImage = findViewById(R.id.progress_waiting_image);
		textCentre = (TextView) findViewById(R.id.label_centre_detail);
		listMode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setTitleMode(TITLE_MAP);
				fromMapMode = false;
				viewFlipper.setDisplayedChild(1);
			}
		});
		mapMode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				viewFlipper.setDisplayedChild(0);
				setTitleMode(TITLE_LIST);
				fromMapMode = true;

			}
		});
		directionMode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Utils.lancerActiviter(MenuCentre.this,
						MenuCentreMapDetail.class);

			}
		});
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.mobile2you.nci.android.utils.BulleDialog.onBullClickListener#onBullClick
	 * (com.mobile2you.nci.android.domain.Centres)
	 */
	@Override
	public void onBullClick(Centres centre) {
		Utils.centreSelected = centre;
		showDetailview(centre);
		setTitleMode(TITLE_DIRECTION);
		fromMapMode = true;
		viewFlipper.setDisplayedChild(2);

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.menu_centre);
		listView = (ListView) findViewById(R.id.list_centre);
		titleCentres = (TextView) findViewById(R.id.label_title_centre);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				final Centres centre = (Centres) adapter.getItem(arg2);
				showDetailview(centre);
				setTitleMode(TITLE_DIRECTION);
				viewFlipper.setDisplayedChild(2);
			}
		});
		mapView = (MapView) findViewById(R.id.map_centre);
		mapMode = findViewById(R.id.view_centre_mapmode);
		listMode = findViewById(R.id.view_centre_listmode);
		directionMode = findViewById(R.id.view_centre_direction);
		setTitleMode(TITLE_MAIN);
		viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
		loaderView = new LoaderView(this);
		viewFlipper.addView(loaderView, 0);
		viewFlipper.setDisplayedChild(0);
		new UpdateLoadCentresTask().execute();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (viewFlipper.getDisplayedChild() > 1) {
				titleCentres.setText(" Centres ");
				if (fromMapMode) {
					setTitleMode(TITLE_LIST);
					viewFlipper.setDisplayedChild(0);
				} else {
					setTitleMode(TITLE_MAP);
					viewFlipper.setDisplayedChild(1);
				}

				return true;
			}
		}

		return super.onKeyDown(keyCode, event);

	}

	private void setTitleMode(int mode) {
		switch (mode) {
		case TITLE_MAIN:
			mapMode.setVisibility(View.GONE);
			directionMode.setVisibility(View.GONE);
			listMode.setVisibility(View.VISIBLE);
			break;
		case TITLE_LIST:
			mapMode.setVisibility(View.GONE);
			directionMode.setVisibility(View.GONE);
			listMode.setVisibility(View.VISIBLE);
			break;

		case TITLE_MAP:
			mapMode.setVisibility(View.VISIBLE);
			directionMode.setVisibility(View.GONE);
			listMode.setVisibility(View.GONE);
			break;

		case TITLE_DIRECTION:
			mapMode.setVisibility(View.GONE);
			directionMode.setVisibility(View.VISIBLE);
			listMode.setVisibility(View.GONE);
			break;

		default:
			mapMode.setVisibility(View.GONE);
			directionMode.setVisibility(View.GONE);
			listMode.setVisibility(View.VISIBLE);
			break;
		}
	}

	private void showDetailview(final Centres centres) {
		titleCentres.setText(centres.getTitle());
		Utils.centreSelected = centres;

		imageView.setVisibility(View.GONE);
		waitingImage.setVisibility(View.VISIBLE);

		textCentre.setText(centres.getTexte());
		if (centres.getBitmap() == null) {
			LoadImagePool.loadImage(this, centres.getImageURL(), new Handler(
					new Callback() {
						@Override
						public boolean handleMessage(Message msg) {
							waitingImage.setVisibility(View.GONE);
							imageView.setVisibility(View.VISIBLE);
							if (msg.obj != null) {
								Bitmap image = (Bitmap) msg.obj;

								imageView
										.setBackgroundDrawable(new BitmapDrawable(
												MenuCentre.this.getResources(),
												image));
								centres.setBitmap(image);

							} else {
								imageView
										.setBackgroundResource(R.drawable.no_image_nci);
							}
							return false;
						}
					}), true);
		} else {
			imageView.setBackgroundDrawable(new BitmapDrawable(MenuCentre.this
					.getResources(), centres.getBitmap()));
		}

		TextView head1 = (TextView) findViewById(R.id.label_head_1);
		head1.setText(centres.getHeading1());

		TextView head1Value = (TextView) findViewById(R.id.label_head_1_value);
		head1Value.setText(centres.getTexte1());

		TextView head2 = (TextView) findViewById(R.id.label_head_2);
		head2.setText(centres.getHeading2());

		TextView head2Value = (TextView) findViewById(R.id.label_head_2_value);
		head2Value.setText(centres.getTexte2());
		Linkify.addLinks(head2Value, Linkify.EMAIL_ADDRESSES | Linkify.ALL);

	}

	private void showListview() {
		adapter = new ListCentreAdapter(listCentres, false);
		listView.setAdapter(adapter);

	}

	private void showMapview() {
		Utils.addmarkerCustom(this, listCentres, mapView, this);
		Utils.zoomInBounds(mapView, listCentres);

	}
}
